define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cash/index' + location.search,
                    // add_url: 'cash/add',
                    // edit_url: 'cash/edit',
                    del_url: 'cash/del',
                    multi_url: 'cash/multi',
                    table: 'cash',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'merchant.merchant_name', title: __('Merchant name'), operate: 'like'},
                        {field: 'merchant.shop_name', title: __('Shop name'), operate: 'like'},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'handle_fee', title: __('Handle_fee'), operate:'BETWEEN'},
                        {field: 'cash_amount', title: __('Cash_amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"1":'待审核', "2":'已通过', "3":'已拒绝'}, formatter: Table.api.formatter.label},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'truename', title: __('Truename'),operate: 'like'},
                        {field: 'alipay_account', title: __('Alipay_account'),operate: 'like'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name:'adopt',
                                    text:'通过',
                                    title:'通过',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url: 'cash/examine?type=2',
                                    confirm: '确认通过？',
                                    visible: function (row) {
                                        if(row.status == 1) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'refuse',
                                    text:'拒绝',
                                    title:'拒绝',
                                    classname:'btn btn-xs btn-danger btn-ajax',
                                    url: 'cash/examine?type=3',
                                    confirm: '确认拒绝？',
                                    visible: function (row) {
                                        if(row.status == 1) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'detail',
                                    text:'订单详情',
                                    title:'订单详情',
                                    icon:'fa fa-list',
                                    classname:'btn btn-xs btn-primary btn-dialog',
                                    url: 'cash/detail'
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //批量通过
            $('.btn-pass').click(function (){
                var order_ids = Table.api.selectedids(table);
                if(order_ids.length > 10){
                    parent.Toastr.error('最多10条');
                }
                layer.confirm(__('确认通过'+order_ids.length+'条数据'),{
                    icon:3,
                    btn: ['确认', '取消']
                },function (index){
                    var ids_str = order_ids.length > 0 ? order_ids.join(",") : 0;
                    Fast.api.ajax({
                        url:'cash/examine',
                        data:{ids:ids_str,type:2}
                    },function (data,ret) {
                        parent.Toastr.success("通过成功！");//成功后提示
                        $("a.btn-refresh").trigger("click");
                        layer.close(index);
                        return false;
                    },function (data,ret) {
                        parent.Toastr.error(ret.msg);
                        layer.close(index);
                        return false;
                    });
                },function (index){
                    layer.close(index);
                });
            });
            //批量拒绝
            $('.btn-refuse').click(function (){
                var order_ids = Table.api.selectedids(table);
                if(order_ids.length > 10){
                    parent.Toastr.error('最多10条');
                }
                layer.confirm(__('确认通过'+order_ids.length+'条数据'),{
                    icon:3,
                    btn: ['确认', '取消']
                },function (index){
                    var ids_str = order_ids.length > 0 ? order_ids.join(",") : 0;
                    Fast.api.ajax({
                        url:'cash/examine',
                        data:{ids:ids_str,type:3}
                    },function (data,ret) {
                        parent.Toastr.success("拒绝成功！");//成功后提示
                        $("a.btn-refresh").trigger("click");
                        layer.close(index);
                        return false;
                    },function (data,ret) {
                        parent.Toastr.error(ret.msg);
                        layer.close(index);
                        return false;
                    });
                },function (index){
                    layer.close(index);
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});